/**
 * 
 */

$(function(){
	
	$("#title").on('keypress', function(event) {
	    if(event.keyCode == 13){
	        $("#btnSearch").click();
	    }
	});
	
	//查询
	$("#btnSearch").click(function(){
		var formSerialize = $("#form").serializeObject();
		var v = JSON.stringify(formSerialize);
		$("#v").val(encodeURIComponent(v));
		form.submit();
	});
	
	
});

function add() {
	
	$.layer({
	    type: 2,
	    shade:  [0.5, '#000'],
	    fix: false,
	    title: '新建品牌',
	    maxmin: false,
	    iframe: {src : global.rootPath + "/partner/brand/add?type=init"},
	    area: ['750px' , '480px'],
	    close: function(index){
	    }
	});
}

function addBrand() {
	var title = $("#title").val();
	var ext1 = $("#ext1").val();
	
	if (null == title || "" == title) {
		layer.tips("中文品牌名不能为空", $("#title") , {guide: 3, time: 3,more:true});
		return false;
	}
	
	var imgUrl = $("#imgUrl").val();
	if (null == imgUrl || "" == imgUrl) {
		layer.tips("图标不能为空", $("#imgUrl") , {guide: 3, time: 3,more:true});
		return false;
	}
	
	
	//ajax file upload
	$.ajaxFileUpload({
		url : global.rootPath + "/partner/brand/add?type=add",
		fileElementId:"logoFile",
		dataType: 'json',
		
		data : {
			title : encodeURIComponent(title),
			ext1 : encodeURIComponent(ext1)
		},
		success: function(data,status) {
			if (data.success) {
				parent.layer.msg(data.msg, 2);
				parent.location.reload();
    			var index = parent.layer.getFrameIndex(window.name);
    			parent.layer.close(index);
    			
			} else {
            	parent.layer.msg(data.msg, 2);
			}
		},
		error:function(data,status,e) {
			parent.layer.msg(data.msg, 3);
		}
	});
	
}

function update(id) {
	
	if (null == id || "" == id ) {
		layer.msg("操作异常", 3);
		return false;
	}
	
	$.layer({
	    type: 2,
	    shade:  [0.5, '#000'],
	    fix: false,
	    title: '修改商家信息',
	    maxmin: false,
	    iframe: {src : global.rootPath + "/partner/brand/update?type=init&id="+id},
	    area: ['750px' , '480px'],
	    close: function(index){
	    }
	});
}

function updateBrand() {
	var title = $("#title").val();
	var ext1 = $("#ext1").val();
	var id = $("#id").val();
	
	if (null == title || "" == title) {
		layer.tips("标题不能为空", $("#title") , {guide: 3, time: 3,more:true});
		return false;
	}
	
	var imgUrl = $("#imgUrl").val();
	
	//ajax file upload
	$.ajaxFileUpload({
		url : global.rootPath + "/partner/brand/update?type=update",
		fileElementId:"logoFile",
		dataType: 'json',
		
		data : {
			title : encodeURIComponent(title),
			ext1 : encodeURIComponent(ext1),
			id : id,
			imgUrl : imgUrl
		},
		success: function(data,status) {
//			parent.location.reload();
//			var index = parent.layer.getFrameIndex(window.name);
//			parent.layer.close(index);
			if (data.success) {
				parent.layer.msg(data.msg, 2);
				parent.location.reload();
    			var index = parent.layer.getFrameIndex(window.name);
    			parent.layer.close(index);
    			
			} else {
            	parent.layer.msg(data.msg, 2);
			}
		},
		error:function(data,status,e) {
			parent.layer.msg(data.msg, 3);
		}
	});
	
}


function cancelBrand(id) {
	security.ajax({
		url : global.rootPath + "/partner/brand/cancel",
		data : {
			id : id
		},
		beforeSend : function(){
			security.showLoading();
		},
		success : function(code,msg) {
			if (msg == "") msg = "注销成功";
			parent.layer.msg(msg, 2);
			parent.location.reload();
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
		},
		error : function(message) {
			security.showError(message);
		}
	});
}


function cancel(id){
	
	if (null == id || "" == id ) {
		layer.msg("操作异常", 3);
		return false;
	}
	
	$.layer({
	    type: 2,
	    shade:  [0.5, '#000'],
	    fix: false,
	    title: '删除品牌',
	    maxmin: false,
	    iframe: {src : global.rootPath + "/partner/brand/detail?id="+id},
	    area: ['750px' , '380px'],
	    close: function(index){
	    }
	});
//	$.layer({
//	    shade: [0],
//	    area: ['auto','auto'],
//	    dialog: {
//	        msg: '是否确定注销该商家信息？',
//	        btns: 2,                    
//	        type: 4,
//	        btn: ['确定','取消'],
//	        yes: function(){
//	        	security.ajax({
//	        		url : global.rootPath + "/adv/partner/cancel",
//	        		data : {
//	        			id : id
//	        		},
//	        		beforeSend : function(){
//	        			security.showLoading();
//	        		},
//	        		success : function(code,msg) {
//	        			if (msg == "") msg = "注销成功";
//	        			security.showSuccess(msg,refresh);
//	        		},
//	        		error : function(message) {
//	        			security.showError(message);
//	        		}
//	        	});
//	        }, no: function(){
//	        }
//	    }
//	});
	
}

function closeIframe() {
	var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

function refresh() {
	var formSerialize = $("#form").serializeObject();
	var v = JSON.stringify(formSerialize);
	$("#v").val(encodeURIComponent(v));
	form.submit();
}